package com.thread.demo.synchronize;

public class SyncAndNoSync {
    /**
     * @param args
     */
    public static void main(String[] args) {

        class Count {

            // 含有synchronized同步块的方法
            public void synMethod() {
                synchronized(this) {
                    try {
                        for (int i = 0; i < 5; i++) {
                            Thread.sleep(100); // 休眠100ms
                            System.out.println(Thread.currentThread().getName() + " synMethod loop " + i);
                        }
                    } catch (InterruptedException ie) {
                    }
                }
            }

            // 非同步的方法
            public void nonSynMethod() {
                try {
                    for (int i = 0; i < 5; i++) {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName() + " nonSynMethod loop " + i);
                    }
                } catch (InterruptedException ie) {
                }
            }
        }

        final Count count = new Count();
        // 新建t1, t1会调用“count对象”的synMethod()方法
        Thread t1 = new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        count.synMethod();
                    }
                }, "t1");

        // 新建t2, t2会调用“count对象”的nonSynMethod()方法
        Thread t2 = new Thread(
                new Runnable() {
                    @Override
                    public void run() {
                        count.nonSynMethod();
                    }
                }, "t2");

        t1.start();  // 启动t1
        t2.start();  // 启动t2
    }
}
